<?php


namespace App\Http\Controllers\App\Game\V1\Lucky;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Models\GameDB\LuckyStrike\RebateUser;
use App\Repositories\Game\Lucky\StrikeRepositories;
use App\Response\Response;
use Illuminate\Http\Request;

class StrikeController extends BaseController
{
    /**
     * 兑换 货币  1 ： 1  配置获取
     * @throws BasicException
     */
    public function exchange(Request $request)
    {
        $automatic = $request->input('automatic') ?? 0;
        $userId = (int)UID;
        if ($automatic == 1 || $automatic == 2) {
            StrikeRepositories::getInstance()->exchange($userId, 0, $automatic);
            return Response::sendData(true);
        }
        $v = app('validator')->make(
            $request->all(),
            [
                'number' => "required|numeric",
            ],
            [
                'required' => ' :attribute必须输入',
                'numeric' => ' :attribute必须为数字'
            ],
            [
                'number' => '兑换数量不能为空',
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $number = intval($request->input('number'));
        if ($number <= 0) {
            throw new BasicException(-1, '非法请求');
        }

        StrikeRepositories::getInstance()->exchange($userId, $number, $automatic);

        return Response::sendData(true);
    }

    /**
     * @param Request $request
     * @return string
     * 获取用户信息
     */
    public function userInfo(Request $request)
    {
        $userId = (int)UID;
        $balance = StrikeRepositories::getInstance()->userInfo($userId, false, true);
        return Response::sendData([
            "balance" => (int)$balance,
        ]);
    }

    /**
     * @param Request $request
     * @return string
     * 获取兑换接口
     * @throws BasicException
     */
    public function setting()
    {
        $userId = (int)UID;
        $setting = StrikeRepositories::getInstance()->setting($userId);
        $user = RebateUser::query()->where("user_id", $userId)->first();
        $automatic = 0;
        if ($user) {
            $automatic = $user['automatic'];
        }
        return Response::sendData([
            "automatic" => $automatic,
            "setting" => explode(',', $setting->setting),
            "balance_coin" => $setting->balance_coin,
        ]);
    }
    /**
     * 点击赠送移到 room服务
     */
}
